package org.example.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;

import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;

public class JWTUtil {
    private final String sign = "yf407";
    private final String secret = "yf407";
    private final Algorithm algorithm = Algorithm.HMAC512(secret);
    
    public String getToken(int level, String identity) throws JWTCreationException {
        JWTCreator.Builder builder = JWT.create();

        return builder.withClaim("level", level)
                .withClaim("identity", identity)
                .sign(algorithm);
    }

    public DecodedJWT verifierToken(String token){
        DecodedJWT verify = null;
        try {
            JWTVerifier verifier = JWT.require(algorithm).build();
            verify = verifier.verify(token);
        }catch (JWTVerificationException jwtVerificationException){
            System.out.println(jwtVerificationException.getMessage());
        }
        return verify;
    }
}
